/**
 * Created by gaoyinxin on 2/21/15.
 */

var Card = cc.Layer.extend({
    _value: 0,
    _bg: null,
    _text: null,
    _textStroke: 2,
    _frozen: false, // cannot be doubled again
    ctor: function (x, y, size) {
        this._super();
        this.setPosition(cc.p(x, y));
        this.setContentSize(size, size);

        this._bg = new cc.LayerColor(cc.color(204, 198, 180));
        this._bg.setPosition(cc.p(0, 0));
        this._bg.setContentSize(size, size);
        this.addChild(this._bg);

        this._text = new cc.LabelTTF("", "Arial", size / 2);
        this._text.setPosition(cc.p(size / 2, size / 2));
        this._text.setContentSize(size, size);
        this.addChild(this._text);
    },
    setValue: function (value) {
        this._value = value;
        switch (value) {
            case 0:
                this._bg.setColor(cc.color(204, 198, 180));
                this._text.setString("");
                this.setLocalZOrder(5);
                break;
            case 2:
                this._bg.setColor(cc.color(228, 228, 214));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color(117, 110, 104));
                this._text.enableStroke(cc.color(117, 110, 104), this._textStroke);
                this._text.setFontSize(this._bg.width / 2);
                this.setLocalZOrder(99);
                break
            case 4:
                this._bg.setColor(cc.color(235, 225, 200));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color(117, 110, 104));
                this._text.enableStroke(cc.color(117, 110, 104), this._textStroke);
                this._text.setFontSize(this._bg.width / 2);
                this.setLocalZOrder(99);
                break;
            case 8:
                this._bg.setColor(cc.color(255, 171, 99));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color.WHITE);
                this._text.enableStroke(cc.color.WHITE, this._textStroke);
                this._text.setFontSize(this._bg.width / 2);
                this.setLocalZOrder(99);
                break;
            case 16:
                this._bg.setColor(cc.color(255, 131, 67));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color.WHITE);
                this._text.enableStroke(cc.color.WHITE, this._textStroke);
                this._text.setFontSize(this._bg.width / 2);
                this.setLocalZOrder(99);
                break;
            case 32:
                this._bg.setColor(cc.color(255, 107, 73));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color.WHITE);
                this._text.enableStroke(cc.color.WHITE, this._textStroke);
                this._text.setFontSize(this._bg.width / 2);
                this.setLocalZOrder(99);
                break;
            case 64:
                this._bg.setColor(cc.color(255, 88, 50));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color.WHITE);
                this._text.enableStroke(cc.color.WHITE, this._textStroke);
                this._text.setFontSize(this._bg.width / 2);
                this.setLocalZOrder(99);
                break;
            case 128:
                this._bg.setColor(cc.color(235, 202, 113));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color.WHITE);
                this._text.enableStroke(cc.color.WHITE, this._textStroke);
                this._text.setFontSize(this._bg.width / 2);
                this.setLocalZOrder(99);
                break;
            case 256:
                this._bg.setColor(cc.color(235, 209, 96));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color.WHITE);
                this._text.enableStroke(cc.color.WHITE, this._textStroke);
                this._text.setFontSize(this._bg.width / 2);
                this.setLocalZOrder(99);
                break;
            case 512:
                this._bg.setColor(cc.color(235, 198, 80));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color.WHITE);
                this._text.enableStroke(cc.color.WHITE, this._textStroke);
                this._text.setFontSize(this._bg.width / 2);
                this.setLocalZOrder(99);
                break;
            case 1024:
                this._bg.setColor(cc.color(235, 195, 63));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color.WHITE);
                this._text.enableStroke(cc.color.WHITE, this._textStroke);
                this._text.setFontSize(this._bg.width * 2 / 5);
                this.setLocalZOrder(99);
                break;
            case 2048:
                this._bg.setColor(cc.color(235, 192, 45));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color.WHITE);
                this._text.enableStroke(cc.color.WHITE, this._textStroke);
                this._text.setFontSize(this._bg.width * 2 / 5);
                this.setLocalZOrder(99);
                break;
            case 4096:
                this._bg.setColor(cc.color(255, 61, 60));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color.WHITE);
                this._text.enableStroke(cc.color.WHITE, this._textStroke);
                this._text.setFontSize(this._bg.width * 2 / 5);
                this.setLocalZOrder(99);
                break;
            case 8192:
                this._bg.setColor(cc.color(255, 61, 60));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color.WHITE);
                this._text.enableStroke(cc.color.WHITE, this._textStroke);
                this._text.setFontSize(this._bg.width * 2 / 5);
                this.setLocalZOrder(99);
                break;
            default:
                this._bg.setColor(cc.color(255, 61, 60));
                this._text.setString(value.toString());
                this._text.setFontFillColor(cc.color.WHITE);
                this._text.enableStroke(cc.color.WHITE, this._textStroke);
                this._text.setFontSize(this._bg.width * 1 / 3.4);
                this.setLocalZOrder(99);
                break;
        }
    },
    getValue: function () {
        return this._value;
    },
    getPos: function () {
        return cc.p(this.x, this.y);
    },
    getCenter: function () {
        return cc.p(this.x + this._bg.width / 2, this.y + this._bg.width / 2);
    },
    getWidth: function () {
        return this._bg.width;
    },
    isFrozen: function () {
        return this._frozen;
    },
    setFrozen: function (b) {
        this._frozen = b;
    },
    clear: function () {
        this.setValue(0);
    },
    double: function () {
        this.setValue(this._value * 2);
    },
    equal: function (cardB) {
        return this._value == cardB._value;
    },
    isEmpty: function () {
        return this._value == 0;
    },
    getClone: function () {
        var copy = new Card(this.x, this.y, this.width);
        copy.setValue(this._value);
        return copy;
    },
    ghostMove: function (target) {
        var deltaX = target.x - this.x;
        var deltaY = target.y - this.y;
        var unit = Math.max(Math.abs(deltaX), Math.abs(deltaY)) / this._bg.width;
        if (unit < 3) return;
        // make ghost flash when move over 3 tablets.
        var copy = this.getClone();
        this.getParent().addChild(copy, 6);
        var time = 0.05 * unit;
        copy._bg.runAction(cc.fadeOut(time));
        copy._text.runAction(cc.fadeOut(time));
        copy.runAction(
            cc.sequence(
                cc.moveBy(time, cc.p(deltaX * 4 / 5, deltaY * 4 / 5)),
                cc.callFunc(copy.removeFromParent, copy, true)
            )
        );
    },
    spark: function () {
        var emitter = new cc.ParticleSystem(res.particle_plist);
        emitter.setPosition(this.getCenter());
        emitter.setPositionType(1);
        this.getParent().addChild(emitter, 200);
        var ratio = this.getWidth() / 200;
        emitter.runAction(
            cc.sequence(
                cc.scaleTo(0, ratio),
                cc.scaleTo(0.25, 0),
                cc.callFunc(emitter.removeFromParent, emitter, true)
            )
        )
    },
    bloat: function () {
        var time = 0.05;
        this.runAction(
            cc.sequence(
                cc.scaleTo(time, 6 / 5),
                cc.scaleTo(time, 1)
            )
        );
    },
    born: function () {
        var time = 0.15;
        this.runAction(
            cc.sequence(
                cc.scaleTo(0, 0),
                cc.scaleTo(time, 1)
            )
        );
    }
});